var app = getApp();
Page(Object.assign({},{
    data:{
        inited : false,
        checkedrule : false,
        agentcode : 0,
        sortname : '',
        cityname : '',
        tradename : '',
        sort : [],
        trade : [],
        city : [],
        current : 0,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;

        self.setData({
            options : options,
        })

        app.com.auth(function(){
            app.com.http('shopinfo_v1','POST',{op:'addshopinfo'},0,false,'','',function(res){
                if (!res.data.errno && res.data) {
                    
                    self.setData({
                        inited : true,
                        params : res.data.data.params,
                        sort : res.data.data.sort,
                        city : res.data.data.city,
                        trade : res.data.data.trade,
                        shopbag : res.data.data.shopbag ? res.data.data.shopbag : [],
                    });

                }else{
                    app.com.alert(res.data.message,function(){
                        app.com.backPage();
                    });
                }

                wx.setNavigationBarTitle({
                    title: '添加'+res.data.data.params.shopbname
                });

            });
        });

    },
    onPullDownRefresh: function () {
        this.onLoad( this.data.options );
        wx.stopPullDownRefresh();
    },
    address(){
        var self = this;
        wx.chooseLocation({
            success : function(res){
                self.setData({
                    lat : res.latitude,
                    lng : res.longitude,
                    address : res.address,
                });
            }
        });
    },
    submit(e){
        var self = this;
        var postdata = {
            op : 'addshop',
            shopname : e.detail.value.shopname,
            tel : e.detail.value.phone,
            pass : e.detail.value.pass,
            pass1 : e.detail.value.pass1,
            lat : self.data.lat ? self.data.lat : 0,
            lng : self.data.lng ? self.data.lng : 0,
            address : self.data.address ? self.data.address : '',
            ctaddress : self.data.ctaddress ? self.data.ctaddress : '',
            sortid : self.data.sortid ? self.data.sortid : 0,
            cityid : self.data.cityid ? self.data.cityid : 0,
            tradeid : self.data.tradeid ? self.data.tradeid : 0,
            bagid : self.data.shopbag[self.data.current].id,
        }
        if( !postdata.tel || !app.com.verify('mobile', postdata.tel) ) {
            app.com.toast('请填写手机');return false;
        }
        if( !postdata.shopname ) {
            app.com.toast('请填写名称');return false;
        }
        if( !postdata.pass ) {
            app.com.toast('请填写登录密码');return false;
        }
        if( postdata.pass.length < 6 || postdata.pass.length > 32 ) {
            app.com.toast('登录密码在6-32位字符之间');return false;
        }
        if( !postdata.lat ) {
            app.com.toast('请选择地址');return false;
        }
        app.com.confirm('添加后名额将减少一个，确定添加吗？',function(){
            app.com.http('ctyagent','POST',postdata,0,true,'','',function(res){
                if( !res.data.errno && res.data ){
                    app.com.alert('已添加成功',function(){
                        app.com.backPage();
                    });
                }else{
                    app.com.alert(res.data.message);
                }
            });
        })
    },
    checkedrule : function(){
        var self = this;
        self.setData({
            checkedrule : !self.data.checkedrule
        })
    },
    selectarea(){
        this.selectComponent('#ausarea').show();
    },
    bindRegionChange : function(e){
        var self = this;
        self.setData({
            ctaddress : e.detail.value[0] +','+e.detail.value[1] +','+e.detail.value[2]
        })
    },
    bindsortChange(e){
        let key = e.detail.value;
        this.setData({
            sortname : this.data.sort[key].name,
            sortid : this.data.sort[key].id,
        })
    },
    bindcityChange(e){
        let key = e.detail.value;
        this.setData({
            cityname : this.data.city[key].name,
            cityid : this.data.city[key].pid,
        })
    },
    bindtradeChange(e){
        let key = e.detail.value;
        this.setData({
            tradename : this.data.trade[key].name,
            tradeid : this.data.trade[key].id,
        })
    },
    bindchange(e){
        this.setData({
            current : e.detail
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    FormIdsubmit : function(e){

    }
}))